package controller;

import java.util.List;
import javax.servlet.http.HttpSession;
import model.Manufacturer;
import model.Product;
import model.User;
import org.apache.log4j.Logger;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import dao.IManufacturerDAO;
import dao.IProductDAO;
import dao.IUserDAO;

@Controller
public class CRUDController {
	
	@Autowired
	@Qualifier("manufacturerServices")
	private IManufacturerDAO manuDao;
	
	@Autowired
	@Qualifier("productServices")
	private IProductDAO productDao;
	
	@Autowired
	@Qualifier("userServices")
	private IUserDAO userDao;
	
	User tempUser = null;
	
	static  Logger log = Logger.getLogger(CRUDController.class.getName());
	
	public CRUDController(){
		log.info("Init CRUD Controller");
	}
	
	@RequestMapping(value = "/editManufacturer", method= RequestMethod.POST)
	@ResponseBody
	public String doEditManufacturer(@RequestBody String requestJson){
		Gson gson = new Gson();
		Manufacturer manufacturer= gson.fromJson(requestJson, Manufacturer.class);
		try {
			log.info("Going to update Manufacturer id ="+manufacturer.getId());
			manuDao.updateManufacturer(
					manufacturer.getName(), 
					manufacturer.getDesc(), 
					manufacturer.getAddress(), 
					manufacturer.getTel(), 
					manufacturer.getWebsite(), 
					manufacturer.getId());
		} catch (Exception e) {
			log.error("Exception",e);
		}
		log.info("Update successfully");
		return "ok";
	}
	
	@RequestMapping(value = "/addManufacturer", method= RequestMethod.POST)
	@ResponseBody
	public String doAddManufacturer(@RequestBody String requestJson,HttpSession session){
		  User usr = (User) session.getAttribute("userInfo");
		  Gson gson = new Gson();
		  Manufacturer manufacturer=gson.fromJson(requestJson, Manufacturer.class);
		  log.info("Begin to add Manufacturer");
		  try {
			  manuDao.addManuFacturer(
					  usr.getId(),
					  manufacturer.getName(), 
					  manufacturer.getDesc(), 
					  manufacturer.getAddress(), 
					  manufacturer.getTel(), 
					  manufacturer.getWebsite());
		} catch (Exception e) {
			log.error("Oops!!! Exception Occured: ", e);
		}
		log.info("Exit to add Manufacturer");
		return "OK";
	}
	
	@RequestMapping(value = "/deleteManufacturer", method= RequestMethod.POST)
	@ResponseBody
	public String doDeletedManufacturer(@RequestBody String id){
		  try {
			  log.info("Going to delete a Manufacturer by id="+id);
			  manuDao.deleteManufacturer(id);
		} catch (Exception e) {
			log.error("Exception",e);
		}
		  log.info("manufacturer deleted ");
		  return "OK";
	}
	
	
	@RequestMapping(value = "/editProduct", method= RequestMethod.POST)
	@ResponseBody
	public String doEditProduct(@RequestBody String requestJson){
		  Gson gson = new Gson();
		  Product product=gson.fromJson(requestJson, Product.class);
		  productDao.updateProduct(
				  (product.getName()),
				  product.getPrice(),
				  product.getDesc(),
				  Integer.parseInt(product.getId()),
				  product.getM_id()
				  );
		  log.info("edit successfully");
		  return "OK";
	}
	
	@RequestMapping(value = "/addProduct", method= RequestMethod.POST)
	@ResponseBody
	public String doAddProduct(@RequestBody String requestJson){
		  Gson gson = new Gson();
		  Product product=gson.fromJson(requestJson, Product.class);
		  productDao.addProduct(
				  product.getName(),
				  product.getPrice(),
				  product.getDesc(),
				  product.getM_id());
		  log.info("product added successfully");
		  return "OK";
	}
	@RequestMapping(value = "/signUp", method = RequestMethod.POST)
	@ResponseBody
	public String signUpAccount(@RequestBody String requestJson){
		Gson gson = new Gson();
		User newUser = gson.fromJson(requestJson, User.class);
		userDao.addUser(newUser.getUsername(), BCrypt.hashpw(newUser.getPassword(), BCrypt.gensalt(12)), newUser.getAddress(), newUser.getBirthday(), newUser.getSex(), newUser.getEmail(), -1);
		log.info("create user successfully");
		return "success";
	}
	
	@RequestMapping(value = "/checkUserNameExist", method = RequestMethod.POST)
	@ResponseBody
	public String checkUserNameHasExist(@RequestBody String requestJson){
		if(userDao.getOneUser(requestJson) != null){
			return "exist";
		}
		return "not exist";
	}
	
	@RequestMapping(value = "/approveUser", method = RequestMethod.POST)
	@ResponseBody
	public String doApproveUser(@RequestBody String requestJson){
		Gson gson = new Gson();
		User approveUser = gson.fromJson(requestJson, User.class);
		userDao.updateUser(approveUser.getUsername(), approveUser.getPassword(), 1, approveUser.getBirthday(), approveUser.getAddress(), approveUser.getSex(), approveUser.getEmail(), Integer.parseInt(approveUser.getId()));
		log.info("finish approve");
		return "success";
	}
	
	
	@RequestMapping(value = "/deleteProduct", method= RequestMethod.POST)
	@ResponseBody
	public String doDeleteProduct(@RequestBody String id){
		  productDao.deleteProduct(id);
		  log.info("product deleted");
		  return "OK";
	}
	
	@RequestMapping(value = "/getNameManuList",method = RequestMethod.GET)
	public @ResponseBody String getNameManuList(@RequestParam("term") String query) {
		List<String> manuNameList=null;
		System.out.println(query);
		try {
			log.info("Getting list of all manufacturer");
			manuNameList = manuDao.getNameManufacturerMatched(query);
		} catch (Exception e) {
			log.error("Exception: ",e);
		}
		log.info("Exit getting list of all manufacturer");
		Gson gson=new Gson();
		return gson.toJson(manuNameList);
	}
	
	@RequestMapping(value = "/getNameProductList",method = RequestMethod.GET)
	public @ResponseBody String getNameProductList(@RequestParam("term") String query) {
		List<String> productNameList=null;
		System.out.println(query);
		try {
			log.info("Getting list of all manufacturer");
			productNameList = productDao.getNameProductMatched(query);
		} catch (Exception e) {
			log.error("Exception: ",e);
		}
		log.info("Exit getting list of all product");
		Gson gson=new Gson();
		return gson.toJson(productNameList);
	}


}
